package cn.hchaojie.camp.javafx.binding;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckboxBinding extends Application {

	@Override
	public void start(Stage stage) throws Exception {
		Scene scene = new Scene(createContents(), 500, 200);
		
		stage.setScene(scene);
		stage.show();
	}
	
	// 在这里创建页面布局
	private Parent createContents() {
		VBox vbox = new VBox(10);
		vbox.setPadding(new Insets(10));
		
		Label label = new Label("您的存款：");

		Slider slider = new Slider(500, 10000, 2000);
		slider.setShowTickLabels(true);
		slider.setMajorTickUnit(2000);
		
		CheckBox ck1 = new CheckBox("买小米");
		CheckBox ck2 = new CheckBox("买华为");
		CheckBox ck3 = new CheckBox("买苹果");
		
		ck1.disableProperty().bind(Bindings.greaterThan(slider.valueProperty(), 1000));
		
		BooleanBinding b1 = Bindings
			.lessThan(slider.valueProperty(), 2000);
		BooleanBinding b2 = Bindings
			.greaterThan(slider.valueProperty(), 5000);

		ck2.disableProperty().bind(b1.and(b2));
		
		ck3.disableProperty().bind(Bindings.lessThan(slider.valueProperty(), 5000));
		
		vbox.getChildren().addAll(label, slider, ck1, ck2, ck3);

		return vbox;
	}

	public static void main(String[] args) {
		launch(args);
	}
}
